<?php

namespace App\Http\Controllers;

use App\Console\Commands\ConsumerKafka;
use App\Http\Models\User;
use Illuminate\Support\Facades\Route;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use App\Http\Services\ProduceService;


class IndexController extends Controller
{

    //prodcut  App\Http\Services\ProduceService
    public function prodcut()
    {
        $obj=new ProduceService();
        $obj->produce();
       // echo __FUNCTION__;
       // exit;
    }
//    public function comsumer()
//    {
//        $obj=new  ConsumerKafka();
//        $obj->handle();
//        echo __FUNCTION__;
//        exit;
//    }
    public function test2()
    {


        exit;
        echo 'test2';
        // Permission::create(['name' => 'api/auth/test']);//创建权限
    }

    public function test()
    {
        Permission::create(['name' => 'api/auth/test8']);//创建权限
    }

    /**
     * 权限测试
     * @author: qitao
     * @date: 2021/12/16
     */
    public function add()
    {
        Permission::create(['name' => 'api/auth/logout']);//创建权限
        $user = User::find(1);
        $user->givePermissionTo('api/auth/logout');
        echo 'add';
        exit;
        // exit;
//        $user = User::find(1);
//        exit;
//        dd($user);
        ////        $request->decodedPath(); //反回的是路径即uri：admin/public
        ////
        ////        $request->route()->getName(); //反回的是路由别名：admin.public.index
        ////        $request->path(); //反回的是路径即uri：admin/public
//        $route = Route::getFacadeRoot()->current()->uri(); //反回的是路径即uri：admin/public
        ////        echo $route;
        ////        exit;
//
//       // Route::currentRouteName(); //反回的是路由别名：admin.public.index
//        echo Route::getCurrentRoute()->getActionName(); //返回完整的Action字符：App\Http\Controllers\Manage\PublicController@index
//        echo '------1----';
//        echo request()->route()->getActionMethod(); //返回: index
        ////        $request->route()->getName(); //反回的是路由别名：admin.public.index
        ////        request()->route()->getName(); //反回的是路由别名：admin.public.index
//        echo '------2----';
//        echo  Route::currentRouteName();
//
//        echo '------:route';
//        exit;
//
//        Permission::create(['name' => 'manage_content9s']);//创建权限
        // exit;
        // 判断用户是否具有一组权限中的任意一个或全部：
        $user = User::find(1);
        $user->givePermissionTo('manage_content6s');
        $rv0 = $user->hasPermissionTo('manage_content6s');
        $re1 = $user->hasAnyPermission(['manage_contents', 'add']);
        $re2 = $user->hasAllPermissions(['manage_contents', 'manage_content2s', 'manage_content6s']);
        // 同样可以仅传入权限的 id
        $re3 = $user->hasAnyPermission(['edit', 1, 5]);

        dd($rv0, $re1, $re2, $re3);

        // 检测用户是否具有特定角色，一个、任意或全部：
        $user = User::find(1);

        $rr1 = $user->hasRole('writer');
        $rr2 = $user->hasAnyRole(Role::all());
        $rr3 = $user->hasAllRoles(Role::all());
        exit;
        // Permission::create(['name' => 'manage_content6s']);//创建权限
        // 检测用户是否有某个权限
        $user = User::where('id', 1)->first();
        $rv0 = $user->hasPermissionTo('manage_content6s');

        // 或者传入权限的 id
        $rv1 = $user->hasPermissionTo(4);
        $rv2 = $user->hasPermissionTo(Permission::find(1)->id ?? 1);

        dd($rv0, $rv1, $rv2);

        exit;
        // Permission::create(['name' => 'manage_content2s']);//创建权限
        $user = User::where('id', 1)->first();

        //$permissions = $user->permissions->toArray();
        $permissions3 = $user->getDirectPermissions()->toArray();;
        dd($permissions3);
        // 获取用户的所有权限，包括直接分配的、通过角色继承的，或者两者全部
        // $permissions = $user->getDirectPermissions();
        $permissions2 = $user->getPermissionsViaRoles()->toArray();
        dd($permissions2);


        // $permissions=$user->getPermissions();
        //   dd($permissions);
//        if($flag){
//            echo 'has ';
//        }else{
//            echo 'no has';
//        }

        // exit;
//        $user->givePermissionTo('manage_content2s');
        $permissions = $user->getDirectPermissions();

        exit;
//        dd($permissions);
        // 单个角色
//       // $user->assignRole('Founder');
//        $user->assignRole('Founder', 'Founder1');
        //// 多个角色
//       // $user->assignRole('writer', 'admin');
//       // print_r(Role::all());
//      //   $user->hasAnyRole(Role::all());
//        $role=Role::findById(2);
//         $role->hasPermissionTo('manage_contents');
//        if(  $role->hasPermissionTo('manage_contents')){
//            echo 'has permission';
//        }else{
//            echo 'no has permission';
//        };
//        $role = Role::create(['name' => 'Founder1']);
//        Permission::create(['name' => 'manage_contents']);//创建权限
//        $role->givePermissionTo('manage_contents');//将权限赋予角色


//        $user = auth('api')->user();
//        print_r($user);
//        $role = Role::get();
//        dd($role);
        // exit;
        // $user->assignRole($role);
        exit;
    }
}
